/*
 * Copyright 2016. chenshufei
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package sz.szsmk.citizencard.ui.dzsmkavtivity;

import android.app.Activity;
import android.app.FragmentTransaction;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import android.text.TextUtils;
import android.view.Menu;
import android.view.MenuItem;

import com.umeng.message.PushAgent;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import sz.szsmk.citizencard.R;
import sz.szsmk.citizencard.dialog.NetRequestTipsDialog;
import sz.szsmk.citizencard.model.network.dzbean.res.MessageEvent;
import sz.szsmk.citizencard.mvp.BasePresenter;
import sz.szsmk.citizencard.mvp.MvpView;
import sz.szsmk.citizencard.util.PubUtils;

/**
 * <br /> author: chenshufei
 * <br /> date: 16/10/26
 * <br /> email: chenshufei2@sina.com
 */
public class BaseActivity extends AppCompatActivity implements MvpView {

    public Context mContext;
    public static String TAG = "BaseActivity";
    protected EventBus eventBus;
    private BasePresenter[] mPresenters;

    private NetRequestTipsDialog mNetRequestTipsDialog;


    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mContext =this;
        String contextString = mContext.toString();
        TAG =  contextString.substring(contextString.lastIndexOf(".") + 1, contextString.indexOf("@"));
        eventBus = EventBus.getDefault();
        PushAgent.getInstance(this).onAppStart();
        eventBus.register(this);
    }

    @Override
    protected void onStart() {
        super.onStart();

    }

    @Override
    protected void onStop() {

        super.onStop();
    }

    @Override
    public void onContentChanged() {
        super.onContentChanged();

        mPresenters = initPresenters();
        //将attachView放在base中搞定
        if (null != mPresenters && mPresenters.length > 0){
            for (BasePresenter presenter : mPresenters){
                if (this instanceof MvpView){
                    presenter.attachView((MvpView)this);
                }
            }
        }
    }

    /**
     * 子类根据实现情况，重写此方法提供presenter出去
     * 若比较简单的页面，直接写死说明，从需要外部数据时，就不需要重写此方法
     * @return
     */
    protected  BasePresenter[] initPresenters(){
        return null;
    };

    public void showLoadingDialog(){
        if (null == mNetRequestTipsDialog){
            mNetRequestTipsDialog = new NetRequestTipsDialog();
        }

        if (!mNetRequestTipsDialog.isAdded()){
            FragmentTransaction ft = getFragmentManager().beginTransaction();
            ft.add(mNetRequestTipsDialog, this.getClass().getSimpleName());
            ft.commitAllowingStateLoss();
        }
    }

    public void dismissLoadingDialog(){
        if (null != mNetRequestTipsDialog ){
            mNetRequestTipsDialog.dismissAllowingStateLoss();
            mNetRequestTipsDialog = null;
        }
    }



    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.qr_menu_main, menu);
        boolean isKeepRight = false;
        String rightTitle = getRightTitle();
        if(!TextUtils.isEmpty(rightTitle)){
            menu.getItem(0).setTitle(rightTitle);
            isKeepRight = true;
        }

        int rightIcon = getRightIcon();
        if (-1 != rightIcon){
            menu.getItem(0).setIcon(rightIcon);
            isKeepRight = true;
        }

        if (!isKeepRight){
            menu.clear();
        }
        return true;
    }

    protected String getRightTitle(){
        return "";
    }

    protected int getRightIcon(){
        return -1;
    }

    protected void onClickRight() {
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_right) {
            onClickRight();
            return true;
            //return super.onOptionsItemSelected(item);
        }

        return super.onOptionsItemSelected(item);
    }


    @Override
    protected void onResume() {
        super.onResume();
        if (null != mPresenters && mPresenters.length > 0){
            for (BasePresenter presenter : mPresenters){
                presenter.onResumeView();
            }
        }
    }

    @Override
    protected void onPause() {
        super.onPause();
        if (null != mPresenters && mPresenters.length > 0){
            for (BasePresenter presenter : mPresenters){
                presenter.onPauseView();
            }
        }
    }

    //子类，自己setTitle进去...

    @Override
    protected void onDestroy() {
        super.onDestroy();
        eventBus.unregister(this);
        if (null != mPresenters && mPresenters.length > 0){
            for (BasePresenter presenter : mPresenters){
                presenter.detachView();
            }
        }
        mPresenters = null;
        dismissLoadingDialog();
    }

    public void launch(Class<? extends Activity> clazz) {
        launch(new Intent(this, clazz));
    }

    public void launch(Intent intent) {
        startActivity(intent);
    }

    @Override
    public void onUnLogin() {
        PubUtils.popTipOrWarn(this,getString(R.string.qr_unlogin));
    }

    public void onEvent(MessageEvent event) {
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onMessageEvent(MessageEvent event) {
        onEvent(event);
    }
}
